package com.zxt.content.job.scheduled;

import com.zxt.content.copywriting.service.CopywritingJobService;
import com.zxt.content.job.scheduled.strategy.EmailPushCopywritingStrategyImpl;
import com.zxt.content.job.scheduled.strategy.PushCopywritingContext;
import com.zxt.content.job.scheduled.strategy.PushCopywritingTypeEnum;
import com.zxt.content.job.scheduled.strategy.WxPushCopywritingStrategyImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


@Slf4j
@EnableScheduling
@Component
@EnableAsync
@AllArgsConstructor
public class CopywritingSchedule {

    private final CopywritingJobService copywritingJobService;

    /**
     * 处理推送文案
     *
     * @Async+@Scheduled 定时任务不阻塞功能，让定时任务开异步线程执行
     */
    @Async
    @Scheduled(cron = "0 0 8 * * ?")
    public void dealPushCopywritingByEmail() {
        pushCopywriting(PushCopywritingTypeEnum.EMAIL.getValue());
    }

    /**
     * 处理推送文案
     *
     * @Async+@Scheduled 定时任务不阻塞功能，让定时任务开异步线程执行
     */
    @Async
    @Scheduled(cron = "0 0 8 * * ?")
    public void dealPushCopywritingByWx() {
        pushCopywriting(PushCopywritingTypeEnum.WX_PUBLIC.getValue());
    }

    /**
     * 通过策略模式推送文案
     *
     * @param value
     */
    private void pushCopywriting(Byte value) {
        PushCopywritingContext context = null;
        if (PushCopywritingTypeEnum.EMAIL.equal(value)) {
            //邮箱推送
            context = new PushCopywritingContext(new EmailPushCopywritingStrategyImpl());
        } else if (PushCopywritingTypeEnum.WX_PUBLIC.equal(value)) {
            //微信公众号推送
            context = new PushCopywritingContext(new WxPushCopywritingStrategyImpl());
        }
        //执行推送
        context.startPush(copywritingJobService);
    }
}
